package Utils;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import View.Game.GamePanel;

/**
 * Make a Button using a image or two.<dd>
 * -Make a animation if are 2 images<br>
 * -Uses the MouseMoutionListener as easy as possible
 * </dd>
 * @author magal
 *
 */

public class ButtonImage implements MouseMotionListener, MouseListener, ConstantsImplement {

	private int x;
	private int y;
	private int width;
	private int heigth;
	private int incrementW;
	private int incrementH;
	private BufferedImage imageL;
	private BufferedImage imageH;
	private boolean up;
	private boolean R2;
	private boolean enable = true;
	private ActionButton action;
	private JPanel panel_;

	public ButtonImage(JPanel panel, String image1) {
		// TODO Auto-generated constructor stub
		panel_=panel;
		x = 0;
		y = 0;
		width = 0;
		heigth = 0;
		incrementW = 0;
		incrementH = 0;
		URL url = GamePanel.class.getResource(image1);
		imageL = Utils.toBufferedImage((new ImageIcon(url)).getImage());
		up = false;
		R2 = false;
	}

	public ButtonImage(JPanel panel,String image1, String image2) {
		// TODO Auto-generated constructor stub
		panel_=panel;
		x = 0;
		y = 0;
		width = 0;
		heigth = 0;
		incrementW = 0;
		incrementH = 0;
		URL url = GamePanel.class.getResource(image1);
		imageL = Utils.toBufferedImage((new ImageIcon(url)).getImage());
		url = GamePanel.class.getResource(image2);
		imageH = Utils.toBufferedImage((new ImageIcon(url)).getImage());
		up = false;
		R2 = false;
	}

	public ButtonImage(JPanel panel,BufferedImage image1, BufferedImage image2, int x, int y) {
		// TODO Auto-generated constructor stub
		panel_=panel;
		this.x = x;
		this.y = y;
		width = 0;
		heigth = 0;
		incrementW = 0;
		incrementH = 0;
		imageL = image1;
		width = imageL.getWidth();
		heigth = imageL.getHeight();		
		imageH = image2;
		up = false;
		R2 = false;
	}

	public ButtonImage(JPanel panel,BufferedImage image1, BufferedImage image2,int x, int y, int width, int heigth) {
		// TODO Auto-generated constructor stub
		panel_=panel;
		this.x = x;
		this.y = y;
		this.width = width;
		this.heigth = heigth;
		incrementW = 0;
		incrementH = 0;
		imageL = image1;
		imageH = image2;
		up = false;
		R2 = false;
	}

	public void mouseDragged(MouseEvent paramMouseEvent) {
		// TODO Auto-generated method stub

	}
	/**
	 * This method verify the mouse location and make an action if the mouse is on the button
	 */
	public void mouseMoved(MouseEvent arg0 ){
		// TODO Auto-generated method stub
		if( arg0.getX() < width + x && arg0.getX() > x &&
				arg0.getY() < heigth + y && arg0.getY() > y){
			//			System.out.println("mouse Entrou em " + this);
			up = true;
			action.enterAction();

		}else{
			up = false;
			//			System.out.println("mouse Saiu de " + this);
			action.exitAction();
		}
	}

	/**
	 * This function draw the button depending if there one or two images.
	 * @param g - Graphics g  to draw the images
	 */
	public void draw(Graphics g){
		if(enable){
			if( !R2 ){
				draw1(g);
			}else{
				draw2(g);
			}
		}
	}

	public void draw1(Graphics g){
		g.drawImage(imageL, x, y, width, heigth, null);
	}

	public void draw2(Graphics g){
		if (up == false){
			g.drawImage(imageL, x, y, width, heigth, null);
		}
		else {
			g.drawImage(imageH, x, y, width + incrementW, heigth + incrementH, null);
		}
	}

	//Getters and Setters
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeigth() {
		return heigth;
	}

	public void setHeigth(int heigth) {
		this.heigth = heigth;
	}

	public int getIncrementW() {
		return incrementW;
	}

	public void setIncrementW(int incrementW) {
		this.incrementW = incrementW;
	}

	public int getIncrementH() {
		return incrementH;
	}

	public void setIncrementH(int incrementH) {
		this.incrementH = incrementH;
	}

	public BufferedImage getImageL() {
		return imageL;
	}

	public void setImageL(BufferedImage imageL) {
		this.imageL = imageL;
	}

	public BufferedImage getImageH() {
		return imageH;
	}

	public void setImageH(BufferedImage imageH) {
		this.imageH = imageH;
	}

	public boolean isUp() {
		return up;
	}

	public void setUp(boolean up) {
		this.up = up;
	}

	public boolean isR2() {
		return R2;
	}

	public void setR2(boolean r2) {
		R2 = r2;
	}

	public void addActionButton(ActionButton action){
		this.action = action;
	}


	//Fim getters and setters

	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if( arg0.getX() < width + x && arg0.getX() > x &&
				arg0.getY() < heigth + y && arg0.getY() > y && enable){
			action.click(arg0);
			R2 = true;
			panel_.repaint();
		}
	}

	public void mousePressed(MouseEvent paramMouseEvent) {
		// TODO Auto-generated method stub

	}

	public void mouseReleased(MouseEvent paramMouseEvent) {
		// TODO Auto-generated method stub

	}

	public void mouseEntered(MouseEvent paramMouseEvent) {
		// TODO Auto-generated method stub

	}

	public void mouseExited(MouseEvent paramMouseEvent) {
		// TODO Auto-generated method stub

	}

	public boolean isEnable() {
		return enable;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}


}
